package com.interesting.examsystem.common.core.controller;

/**
 * @author ：ZXY
 * @date ：Created in 2021/2/22 17:44
 * @description：基础控制器
 * @modified By：`
 * @version: 1.0
 */

import cn.hutool.core.util.StrUtil;
import com.interesting.examsystem.common.constant.RestConstant;
import com.interesting.examsystem.common.utils.RequestContextUtil;
import com.interesting.examsystem.common.utils.SpringContextUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class BaseController {
    protected HttpServletRequest getRequest() {
        return RequestContextUtil.getRequest();
    }

    protected HttpSession getSession() {
        HttpServletRequest request = getRequest();
        return request.getSession(true);
    }

    public HttpServletResponse getResponse() {
        return RequestContextUtil.getResponse();
    }

    public String getOperatorId() {
        String token = getRequest().getHeader(RestConstant.X_ACCESS_TOKEN);
//        RedisTemplateUtil redisUtil = (RedisTemplateUtil) SpringContextUtil.getBean("redisTemplateUtil");
//        String operatioId = (String) redisUtil.getMapValueFromCache("PREFIX_USER_INFO_" + token,
//                "loginId");
        String operatioId = "";
        return StrUtil.isEmpty(operatioId) ? "default" : operatioId;
    }

    public List<String> getRoles() {
        String token = getRequest().getHeader(RestConstant.X_ACCESS_TOKEN);
//        RedisTemplateUtil redisUtil = (RedisTemplateUtil) SpringContextUtil.getBean("redisTemplateUtil");
//        return (List<String>) redisUtil.getMapValueFromCache("PREFIX_USER_INFO_" + token, "roles");
        return  new ArrayList<>();
    }

    private List<String> getPatternStr(String str) {
        List<String> list = new ArrayList<String>();
        if (StrUtil.isNotBlank(str)) {
            String rex = "(?<=\").*?(?=\")";
            Pattern p = Pattern.compile(rex);
            Matcher m = p.matcher(str);
            while (m.find()) {
                String findStr = m.group();
                if (!",".equals(findStr) && !"，".equals(findStr)) {
                    list.add(findStr);
                }
            }
        }
        return list;
    }
}
